package com.ids.transfer.item;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;
import java.util.HashMap;
import com.ids.transfer.AbtractExtra;
import com.ids.transfer.ExtraExecute;
import com.ids.transfer.ExtraParam;
import com.ids.transfer.config.Constant;
import com.ids.transfer.config.DatasourceConfig;
import com.ids.transfer.config.ItemConfig;
import com.ids.transfer.config.TaskConfig;
import com.ids.transfer.util.DbUtils;
import com.ids.transfer.util.ExtraUtils;
import com.ids.transfer.util.StrCharUtil;

/**
 * 一般数据抽取，主要处理select和insert
 * @author gold
 *
 */
public class UpdateInsertItem extends AbtractExtra // 必须继承自AbtractEtra抽取类
{

	private ExtraParam ep;
	
	public void beginEtra() {
		if (logger.isInfoEnabled()) {
			logger.info("start of Extra-data!");
		}
		
	}

	public void endEtra() {
		
		if (logger.isInfoEnabled()) {
			logger.info("end of Extra-data!");
		}
	}

	public void executeExtra() {
		DatasourceConfig srcDsc, desDsc; // 源和目的数据源信息配置类
		//这一层放在外面来控制
		HashMap<String,ItemConfig> items;
		//实现类可能对应的是同一个类，
		ItemConfig item;
		TaskConfig tc; // 任务配置信息
		HashMap param = ep.getParam();

		// 通过强制类型转化得到所有的数据抽取前的初始化信息
		srcDsc = (DatasourceConfig) param.get(ExtraParam.SRCDSC);
		desDsc = (DatasourceConfig) param.get(ExtraParam.DESDSC);
		//只是xml里的配置信息
		items = (HashMap<String, ItemConfig>) param.get(ExtraParam.ITEMS);
		tc = (TaskConfig) param.get(ExtraParam.TASKCONFIG);
		// 此处书写你自己的代码:自定义的参数，如果没有，就不必写,大多数情况下忽略此部分代码的书写
		
		
		// 得到源和目的JDBC连接,用到的JDBC驱动必须在CLASSPATH路径中
		Connection srcConn = null;
		Connection desConn = null;
		try {
			srcConn = srcDsc.getConnection();
			desConn = desDsc.getConnection();
			// String itemName=getName();

				item=items.get(getName());
				logger.info("任务ID:" + tc.getId() + ",实现类:" + item.getType());
				// ---------------------------------

				HashMap sqlMap = item.getPmap();
				int num = item.getModuleNum();
				String[] srcFroms = new String[num];
				String[] sqlTos = new String[num];
				String[] sqlFromPks = new String[num];
				String[] sqlToPks=new String[num];
				for (int i = 0; i < num; i++) {
					srcFroms[i] = (String) sqlMap.get("m" + i + "_from");
					sqlTos[i] = (String) sqlMap.get("m" + i + "_to");
					sqlFromPks[i] = (String) sqlMap.get("m" + i + "_from_pk");
					sqlToPks[i]=(String) sqlMap.get("m" + i + "_to_pk");
					System.out.println(srcFroms[i]);
					System.out.println(sqlTos[i]);
					System.out.println("from pk"+sqlFromPks[i]);
					System.out.println("to pk"+sqlToPks[i]);
					//任一主键为空执行插入操作，从更大的角度讲
					if(StrCharUtil.isNull(sqlFromPks[i])
							||StrCharUtil.isNull(sqlToPks[i])){
						ExtraExecute.batchRs(srcFroms[i], srcConn, desConn,
								sqlTos[i], Constant.BATCHNUM);
					}else{
						//执行更新操作
						ExtraExecute.batchRs(srcFroms[i],sqlFromPks[i], srcConn, desConn,
								sqlTos[i],sqlToPks[i], Constant.BATCHNUM);
					}
					
				}
		} catch (Exception e) {
			try {
				desConn.rollback();
				logger.error("执行异常，rollback success!");
			} catch (SQLException e1) {
				logger.error("执行异常，rollback failure!");
				e1.printStackTrace();
			}
			logger.error(e);
		} finally {
			DbUtils.close(srcConn);
			DbUtils.close(desConn);
		}
		
		
		
	}

	// 抽取函数
	public void extra() {
		beginEtra(); // 开始抽取准备
		executeExtra(); // 数据抽取
		endEtra(); // 抽取结束工作
	}

	@Override
	public void setParams(ExtraParam ep) {
		// TODO Auto-generated method stub
		if (this.ep == null)
			this.ep = ep;
		else {
			this.ep.addParam(ep);
		}
	}

	@Override
	public ExtraParam getParams() {
		// TODO Auto-generated method stub
		return ep;
	}



}